package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.managerDao;
import com.example.yiwei.crm.dao_impl.managerDaoImpl;
import com.example.yiwei.crm.data.GetCustomerData;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.entity.managerEntity;
import com.example.yiwei.crm.json.CoorCustomerJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CoordingCustomerSearchActivity extends BaseActivity {
ImageView img_back,img_commit;
    private TextView txt_create_time1,txt_create_time2;
    private EditText edit_name,edit_phone;
    private RadioGroup rg;
    private RadioButton rb1,rb2;
    private Button btn_show1,btn_show2;
    private List<CustomerMessageEntity> alist;
    private ArrayAdapter adapter1;
    private TimePickerView mpv;
    private TimePickerView mpv1;
    private String create_time1,create_time2;
    private List<String> name_list=new ArrayList<>();
    List<managerEntity> managerList;
    private Spinner spinner_manager;
    int manager_id;
    SharedPreferences sp;
    int sex;
    Date date1,date2;
    String managerName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coording_customer_search);
        spinner_manager= (Spinner) findViewById(R.id.coording_customer_search_spinner_manager);
        btn_show1= (Button) findViewById(R.id.coording_customer_search_btn_show1);
        btn_show2= (Button) findViewById(R.id.coording_customer_search_btn_show2);
        img_back= (ImageView) findViewById(R.id.coording_customer_search_img_back);
        img_commit= (ImageView) findViewById(R.id.coording_customer_search_img_commit);
        txt_create_time1= (TextView) findViewById(R.id.coording_customer_search_txt_create_time1);
        txt_create_time2= (TextView) findViewById(R.id.coording_customer_search_txt_create_time2);
        edit_name= (EditText) findViewById(R.id.coording_customer_search_edit_name);
        edit_phone= (EditText) findViewById(R.id.coording_customer_search_edit_mobile_phone);
        rg= (RadioGroup) findViewById(R.id.coording_customer_search_rg);
        rb1= (RadioButton) findViewById(R.id.coording_customer_search_rb_men);
        rb2= (RadioButton) findViewById(R.id.coording_customer_search_rb_women);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //创建选择器
        mpv = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv.setTime(new Date());
        //选择监听
        mpv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date1 = date;
                create_time1=getTime(date);
                txt_create_time1.setText(create_time1);
            }
        });
        btn_show1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv.show();
            }
        });
        //创建选择器
        mpv1 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar1 = Calendar.getInstance();
        mpv1.setRange(calendar1.get(Calendar.YEAR) - 100, calendar1.get(Calendar.YEAR) + 100);
        //插入时间
        mpv1.setTime(new Date());
        //选择监听
        mpv1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date2 = date;
                create_time2=getTime(date);
                txt_create_time2.setText(create_time2);
            }
        });
        btn_show2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv1.show();
            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==rb1.getId()){
                    sex=1;
                }else{
                    sex=2;
                }
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("admin",sp.getString("admin",null));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                managerDao manaDao=new managerDaoImpl();
                managerList=manaDao.queryAllManager(baseClass.query_manager_url,json);
                name_list.add(0,"");
                for (int i2=0;i2<managerList.size();i2++){
                    name_list.add(managerList.get(i2).getManager_name());
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter1 = new ArrayAdapter(CoordingCustomerSearchActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, name_list);
                        adapter1.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_manager.setAdapter(adapter1);
                        spinner_manager.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                managerName=spinner_manager.getSelectedItem().toString();
                                for (int i1= 0;i<managerList.size();i++){
                                    if (managerName.equals(managerList.get(i1).getManager_name())){
                                        manager_id = Integer.valueOf(managerList.get(i1).getManager_id());
                                    }
                                }
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {

                            }
                        });
                    }
                });
            }
        }).start();
    img_commit.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
//         Intent intent=new Intent(CoordingCustomerSearchActivity.this,CoorCustomerActivity.class);
//         intent.putExtra("name",edit_name.getText().toString());
//         intent.putExtra("phone",edit_phone.getText().toString());
//         intent.putExtra("create1",txt_create_time1.getText().toString());
//         intent.putExtra("create2",txt_create_time2.getText().toString());
//         intent.putExtra("sex",sex);
//         intent.putExtra("manager_id",manager_id);
//         intent.putExtra("tag","search");
//         intent.putExtra("dock_id",getIntent().getExtras().getInt("dock_id"));
//         startActivity(intent);
        if (date1!=null&&date2!=null&&date1.after(date2)){

                Toast.makeText(CoordingCustomerSearchActivity.this, "开始时间不能再结束时间之后", Toast.LENGTH_SHORT).show();
            }else {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SC sc = new SC();
                        JSONObject object = sc.storage(sp);
                        try {
                            object.put("KXA01",getIntent().getExtras().getInt("dock_id"));
                            object.put("KAA09",manager_id);
                            object.put("KAA04",sex);
                            object.put("CID01",null);
                            object.put("KAA02",edit_name.getText().toString());
                            object.put("KLA06",edit_phone.getText().toString());
                            object.put("CTS01A",txt_create_time1.getText().toString());
                            object.put("CTS01B",txt_create_time2.getText().toString());
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        String json = object.toString();
                        String result = NetWorkUtils.getCount(baseClass.find_coor_allcustomer_URL+1,json);
                        alist = CoorCustomerJson.jsonVisitMessage(result);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (alist!=null){
                                    GetCustomerData.crateData();
                                    GetCustomerData.customers=alist;
                                    finish();
                                }else {
                                    Toast.makeText(CoordingCustomerSearchActivity.this, "此条件无值", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }).start();
            }
        }
     }
 );
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}
